package sc.prototype.pfs.test.eventnotify.server;

import java.util.HashMap;

public class MockServerToolkit {

    static HashMap<ServerKey, MockServer> serverMap;

    public static void tryRunServer(int port, RequestHandler handler) {
        if (serverMap == null) {
            serverMap = new HashMap<ServerKey, MockServer>();
        }
        ServerKey key = new ServerKey(port, handler);
        if (serverMap.containsKey(key)) {
            return;
        } else {
            MockServer mockServer = new MockServer(port, handler);
            mockServer.run();
            serverMap.put(key, mockServer);
        }
    }

    public static class ServerKey {
        public ServerKey(int port, RequestHandler handler) {
            this.port = port;
            this.handler = handler;
        }

        int port;
        RequestHandler handler;

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof ServerKey)) {
                return false;
            }
            ServerKey cpKey = (ServerKey) obj;
            return this.port == cpKey.port && this.handler == cpKey.handler;
        }
    }
}
